package com.cloud.wms.ui.returned.inventory;

import android.content.Context;
import android.content.Intent;
import android.os.Message;
import android.view.View;

import androidx.annotation.NonNull;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.cloud.wms.R;
import com.cloud.wms.base.BaseBarListActivity;
import com.cloud.wms.config.IntentKey;
import com.cloud.wms.ui.returned.contact.InventoryContract;
import com.cloud.wms.ui.returned.entity.InventoryDetailEntity;
import com.cloud.wms.ui.returned.presenter.InventoryPresenter;
import com.cloud.wms.widget.dialog.AlertDialogBuilder;

import java.util.ArrayList;

/**
 * @Author： Chen
 * @Date： 2021/5/19
 * @Desc：
 */
public class InventoryDetailActivity extends BaseBarListActivity<InventoryDetailEntity, InventoryPresenter> implements InventoryContract.View {
    @Override
    protected String barTitle() {
        return "清点明细";
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        mPresenter.getReturnedInventoryDetail(getIntent().getStringExtra(IntentKey.INFO_KEY));
        mAdapter.addChildClickViewIds(R.id.tv_inventory_delete);
        mAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                InventoryDetailEntity detailEntity = (InventoryDetailEntity) adapter.getData().get(position);
                new AlertDialogBuilder(InventoryDetailActivity.this)
                        .setTitle("删除确认")
                        .setMessage("是否删除商品：" + detailEntity.getBarCode() + "?")
                        .setPositiveButton("删除", (dialog, which) -> mPresenter.deleteInventoryCommodity(getIntent().getStringExtra(IntentKey.INFO_KEY), detailEntity))
                        .setNegativeButton("取消", null)
                        .show();
            }
        });
    }

    @Override
    public void onReload() {
        super.onReload();
        mPresenter.getReturnedInventoryDetail(getIntent().getStringExtra(IntentKey.INFO_KEY));
    }

    @Override
    protected int getItemResId() {
        return R.layout.item_returned_inventory_detail;
    }

    @Override
    protected void bindItemData(BaseViewHolder helper, InventoryDetailEntity item) {
        helper.setText(R.id.tv_commodity_name, item.getCommodityName())
                .setText(R.id.tv_commodity_code, item.getBarCode())
                .setText(R.id.tv_box_code, item.getBoxCode())
                .setText(R.id.tv_commodity_batch, item.getDefaultBatch())
                .setText(R.id.tv_commodity_amount, item.getAmount() + "")
                .setText(R.id.tv_commodity_type, item.getStockTypeName())
                .setBackgroundResource(R.id.tv_commodity_type, item.getStockType() == 1 ? R.drawable.shape_in_detail_good : R.drawable.shape_in_detail_bad);
    }

    @Override
    protected int getResId() {
        return R.layout.activity_returned_inventory_detail;
    }

    @Override
    protected void setupComponent() {
        getActivityComponent().inject(this);
    }

    @Override
    public void onSuccess(Message message) {
        super.onSuccess(message);
        switch (message.what) {
            case InventoryPresenter.GET_INVENTORY_COMMODITY:
                mAdapter.setNewInstance(((ArrayList<InventoryDetailEntity>) message.obj));
                break;
            case InventoryPresenter.DELETE_INVENTORY_COMMODITY:
                success("删除成功！");
                mPresenter.getReturnedInventoryDetail(getIntent().getStringExtra(IntentKey.INFO_KEY));
                break;
        }
    }

    public static void start(Context context, String carryCode) {
        Intent intent = new Intent(context, InventoryDetailActivity.class);
        intent.putExtra(IntentKey.INFO_KEY, carryCode);
        context.startActivity(intent);
    }
}
